<?php

class Articles extends CI_Controller
{
    var $data = array ();
    var $translate;

    function __construct ()
    {
        parent::__construct ();

        if ( !$this->session->userdata ( 'logged_in' ) )
            redirect(site_url('login'));

        $this->load->model ( 'articles_model' );
        $this->load->model ( 'pages_model' );

        $this->load->helper ( 'inflector' );
        $this->load->config ( 'languages' );
        //$this->translate = & $this->factory->create ( 'translate', 'foo', 'bar' );
        $this->lang->load('common');
    }

    function index ()
    {
        $this->view_all();
    }
    public function view_all ()
    {
        $articles = $this->articles_model->list_all();

        $this->firephp->log($articles);


        $this->data['articles'] = $articles;
        $this->data['title'] = "All Articles";
        $this->data['page_title'] = "Articles";

        $this->template->build ( 'articles/view_all', $this->data );
    }

    public function view_new_article()
    {
        $pages = $this->pages_model->find_all();
		
		$this->load->model('pictures_model');
        $pictures = $this->pictures_model->list_all();

        $this->data = array_merge ( $this->data, array (
            'post_action' => 'NEW',
            'pages' => $pages,
			'pictures' => $pictures
        ) );
        $this->template->build ( 'articles/new_edit_article', $this->data );
    }

    public function view_edit_article($article_id)
    {
        $pages = $this->pages_model->find_all();
		
		$this->load->model('pictures_model');
        $pictures = $this->pictures_model->list_all();
		
        $article = $this->articles_model->find_id($article_id);
        $this->data = array_merge ( $this->data, array (
            'post_action' => 'EDIT',
            'pages' => $pages,
			'pictures' => $pictures,
            'article' => $article
        ) );
        $this->template->build ( 'articles/new_edit_article', $this->data );
    }

    public function submit()
    {
        $this->firephp->log($_POST);

        //Post data sent by view
        $post = $_POST;

        $post_action = $post['post_action'];

        $data = array (
            'content' => $post['content'],
            'title' => $post['title'],
			'picture_id' => $post['picture_id'],
            'page_id' => $post['page_id']
        );

        $insert_id = null;

        if($post_action == "NEW")
        {
            $this->db->trans_start ();
            $insert_id = $this->articles_model->insert ( $data );
            $this->db->trans_complete ();
        }
        else
        {
            $this->db->trans_start ();
            $id = $post['article_id'];
            $insert_id = $this->articles_model->update($id, $data);
            $this->db->trans_complete ();

        }

        if($insert_id){
            echo json_encode ( array (
                "action" => true,
                "msg" => lang('common.successfullyoperation'),
                "insert_id" => $insert_id ) );
        }
        else
        {

            // If comes to thisd line, error ocured
            echo json_encode ( array (
                'action' => FALSE,
                'msg' => '<p>' . lang('common.thisshouldnothappen') . '</p>'
            ) );
        }

    }

    function delete_article ( $article_id )
    {
        $this->articles_model->delete ( $article_id );
    }



}
?>